import axios from 'axios'
export default {
  install: function(Vue, options) {

    //跨域请求
    Vue.prototype.$ajax = axios
    Vue.prototype.$tldJsonp = function(options) {
        if (options.isLayer) {
          this.$store.commit('updateLoading');
        }
        this.$http.jsonp(options.url, {
          params: options.params
        }).then((response) => {
          if (options.isLayer) {
            this.$store.commit('updateLoading')
          }
          if (response.data.success) {
            //成功回调
            if (options.successFc && typeof(options.successFc) == "function") {
              options.successFc(response);
            }
          } else {
            this.$store.commit('updateToast', response.data.msg);
            //失败回调
            if (options.failFc && typeof(options.failFc) == "function") {
              options.failFc(response);
            }
          }
        }, (response) => {
          if (options.isLayer) {
            this.$store.commit('updateLoading')
          }
          //没有登陆
          //          this.$Message.error('登录过期,即将跳转登陆');
          this.$router.push({
            path: '/'
          });
        });
      },
      Vue.prototype.$tldAjax = function(options) {
        if (options.isLayer) {
          this.$store.commit('updateLoading');
        }
        this.$ajax({
          // method: options.method,
          url: options.url,
          params: options.params,
          withCredentials: true
        }).then((response) => {
          if (options.isLayer) {
            this.$store.commit('updateLoading')
          }
          if (response.data.success) {
            //成功回调
            if (options.successFc && typeof(options.successFc) == "function") {
              options.successFc(response);
            }
          } else {
            this.$store.commit('updateToast', response.data.msg);
            //失败回调
            if (options.failFc && typeof(options.failFc) == "function") {
              options.failFc(response);
            }
          }
        }, (response) => {
          if (options.isLayer) {
            this.$store.commit('updateLoading')
          }
          //没有登陆
          //          this.$Message.error('登录过期,即将跳转登陆');
          this.$router.push({
            path: '/'
          });
        });
      },
      Vue.prototype.$domain = process.env.DOMAIN;
  }
};
